<template>
	<view>
		<view class="selectMember">
			<view class="memberBox">
				<view class="member" @click="dialog = true">
					<image src="../../../static/img/quickly/information/add@2x.png"></image>
					<text>{{userName || '选择会员'}}</text>
				</view>
				<view class="save" v-if="tabType == 1 && !reportNumber">
					<span v-if="!reportUrl && medicalStatus != 2" @click="save(1)">签发</span>
				</view>
				<view class="save" v-if="tabType == 2 && !questionNumber">
					<span  v-if="!reportUrl && questionStatus != 2" @click="save">签发</span>
				</view>
			</view>
			
			<view class="tabs">
				<view :class="['tabs-item',tabType == 1?'active':'']" @click="[tabType = 1,tabchange(1)]">体检数据录入</view>
				<view :class="['tabs-item',tabType == 2?'active':'']" @click="[tabType = 2,tabchange(2)]">生活方式问卷</view>
				<view :class="['tabs-item',tabType == 3?'active':'']" @click="[tabType = 3,tabchange(3)]">评估报告</view>
			</view>
		</view>
		
		<!-- 体检数据录入 -->
		<view class="mainContent">
			<addassess2 ref="addassess" v-show="tabType == 1"  :userId="userId"></addassess2>
			<investigation2 ref="investigation" v-show="tabType == 2"  :userId="userId"></investigation2>
			<div class="uploadReport" v-show="tabType == 3">
				<div class="item">
					<p>体检报告:{{reportNumber || ''}}</p>
					<p v-if="reportStatus == 2">已上传</p>
					<p v-else>{{reportNumber?'已完善':'未完善'}}</p>
					<span v-if="!reportStatus || reportStatus == 1" @click="uploadReport(reportNumber)"><image mode="widthFix" src="@/static/img/createreport-upload.png"></image></span>
				</div>
				<div class="item">
					<p>问卷调查:{{questionNumber || ''}}</p>	
					<p v-if="questionStatus == 2">已上传</p>
					<p v-else>{{questionNumber?'已完善':'未完善'}}</p>
					<span v-if="!questionStatus || questionStatus == 1" @click="uploadquestion(questionNumber)"><image mode="widthFix" src="@/static/img/createreport-upload.png"></image></span>
				</div>
				<button class="creat" @click="creatReport()" v-if="reportStatus == 2 && questionStatus == 2 && status != 2">生成评估报告</button>
				<button class="uncreat" v-else-if="reportStatus != 2 || questionStatus != 2">生成评估报告</button>
				<button class="read" @click="read()" v-if="status == 2" style="background: #34bb46;">查看报告报告</button>
			</div>
		</view>

		<!-- 会员弹框 -->
		<member :dialog.sync="dialog"  @hidedialog="hidedialog" @chooseUser="chooseUser"></member>
	</view>
</template>

<script>
	import addassess2 from '@/pages/quickly/assessment/addassess2.vue';
	import investigation2 from '@/pages/quickly/assessment/investigation2.vue'
	import member  from '@/components/selectMember/selectMember.vue';
	import {getUrlParam,filterData2} from '@/static/js/filter.js'
	export default {
		components:{member,addassess2,investigation2},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				dialog: false,
				searchContent: '', //会员输入框
				userName:'',
				current: '',
				list:[],
				tabType: 1, //tab选项
				userId:'',
				canAdd:true ,//是否能添加问卷
				questionBtn:true,
				reportBtn:true,
				creatReportStatus:false,
				reportUrl:'',
				medicalStatus:'',
				questionStatus:'',
				canSetUserId:true,
				AssessReportStatus:{},
				hasData:'',
				reportNumber:'',
				medicalTime:'',
				medicalName:'',
				questionNumber:'',
				reportStatus:'',
				questionStatus:'',
				status:''
			}
		},
		onLoad(options){
			if(options.data){
				this.optionData =  JSON.parse(options.data)
				const data = JSON.parse(options.data)
				this.userId = data.userId
				this.userName = data.realName
				this.questionNumber = data.questionNumber
				this.reportNumber = data.reportNumber
				this.medicalName = data.medicalName
				this.reportDate = data.reportDate
				this.reportNo = data.reportNo
				this.status = data.status
				setTimeout(()=>{
					this.$refs.addassess.userChange(data.userId,data.reportNumber,data.sex)
					this.$refs.investigation.userChange(data.userId,data.questionNumber,data.sex)
				},1000)
			}
		},
		methods:{
			// tab改变
			tabchange(e){
				if(e == 3){
					if(!this.userId){
						return;
					}
					if(this.optionData){
						console.log(this.optionData)
						this.reportNumber = this.optionData.reportNumber
						this.questionNumber = this.optionData.questionNumber
						this.questionId = this.optionData.questionId
						this.questionTime = this.optionData.questionTime
						this.reportStatus = this.optionData.reportStatus
						this.questionStatus = this.optionData.questionStatus
						this.status = this.optionData.status
						return;
					}
					this.$api.Assessreport(this.userId).then((res)=>{
						this.reportNumber = res.data.result && res.data.result.reportNumber
						this.questionNumber = res.data.result && res.data.result.questionNumber
						this.questionId = res.data.result && res.data.result.questionId
						this.questionTime = res.data.result && res.data.result.questionTime
						this.reportStatus = res.data.result && res.data.result.reportStatus
						this.questionStatus = res.data.result && res.data.result.questionStatus
					})
				}
			},
			hidedialog(){
				this.dialog = false
			},
			chooseUser(e){
				this.userId = e.userId;
				this.userName = e.userName;
				this.$refs.addassess.userChange(e.userId,'',e.sex)
				this.$api.Assessreport(e.userId).then((res)=>{
					this.reportNumber = res.data.result && res.data.result.reportNumber
					this.questionNumber = res.data.result && res.data.result.questionNumber
					this.$refs.investigation.userChange(e.userId,res.data.result && res.data.result.questionNumber)
				})
			},
			getUrlParam,
			// 保存
			save(){
				if(this.tabType == 1){
					this.$refs.addassess.submit()
				}else if(this.tabType == 2){
					this.$refs.investigation.submit()
				}
			},
			// 子组件体检数据返回
			getAssessData(data){
				this.AssessData = data
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
			
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			radioChange(e) {
				this.list && this.list.map((res) => {
					if (res.userId == e.detail.value) {
						this.userName = res.realName;
						this.userId = res.userId
					}
				})
				this.$api.checkoutParam({userId:this.userId}).then((res)=>{
					if(res.data.returnCode != 200){
						this.canAdd = false
					}else{
						this.canAdd = true
					}
				})
			},
			setuserIdstatus(){
				this.canSetUserId = false;
			},
			// 生成评估报告
			creatReport(){
				uni.showLoading({
					title: '生成中...'
				})
				const _this = this;
				this.$api.AssesscreateReport({
					reportNumber: _this.reportNumber
				}).then(res => {
					uni.hideLoading()
					if (res.data.returnCode == 200) {
						_this.reportURl = res.data.message
						uni.showToast({title: '生成成功',icon: 'none'});
						_this.status = 2
						// _this.$set(_this,'reportUrl',res.data.Msg)
					}else{
						uni.showToast({title: res.data.result ,icon: 'none'});
					}
				})
			},
			// 上传体检到希和
			uploadReport(reportNumber){
				if(reportNumber == 'null' || !reportNumber){
					uni.showToast({title: '请先完善体检报告',icon: 'none'});
					return;
				}
				const _this = this;
				uni.showModal({
					title: '提示',
					content: '上传后不可再修改健康体检数据，请确认数据完整正确。',
					success: function(res) {
						if (res.confirm) {
							_this.$api.uploadHealthAddUserReport({
								"reportConclusionS": [
									{"sectionName": "一般检查"},
									{"sectionName": "生化检测"},
									{"sectionName": "营养素"},
									{"sectionName": "肿瘤检测"},
								 ],
								userId:_this.userId,
								reportNumber:reportNumber,
								"questionId": _this.questionId,
								"questionNumber": _this.questionNumber,
								"questionTime": _this.questionTime,
								"questionType":1
							}).then((res)=>{
								if(res.data.returnCode == 200){
									_this.reportStatus = 2
									uni.showToast({title: '上传成功',icon:'none'});
								}else{
									uni.showToast({title: res.data.message,icon:'none'});
								}
							})
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			},
			// 上传问卷到希和
			uploadquestion(questionNumber){
				if(questionNumber == 'null' || !questionNumber){
					uni.showToast({title: '请先完善问卷调查',icon: 'none'});
					return;
				}
				const _this = this;
				uni.showModal({
					title: '提示',
					content: '上传后不可再修改问卷数据，请确认数据完整正确。',
					success: function(res) {
						if (res.confirm) {
							_this.$api.addQuestionnaireByUser({id:_this.userId,questionNumber:questionNumber}).then((res)=>{
								if(res.data.returnCode == 200){
									_this.questionStatus = 2
									uni.showToast({title: '上传成功',icon: 'none'})
								}else{
									uni.showToast({title: res.data.message,icon: 'none'})
								}
							})
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			},
			// 查看报告
			read(){
				window.location.href = 'http://health.jlm12580.com/web/viewer.html?file='+this.reportURl
			},
			//健康师获取用户评估报告状态
			getAssessReportAxios(){
				if(this.userId){
					this.$api.getAssessReport(this.userId).then((res)=>{
						this.AssessReportStatus = res.data.Result
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	@import '@/static/css/quickly/assessment/createreport.scss';
	.selectMember{
		z-index: 100;
	}
</style>
